package com.ler.yrmall.coupon.controller;

import com.ler.yrmall.basic.Form;
import com.ler.yrmall.basic.Result;
import com.ler.yrmall.coupon.entity.CouponEntity;
import com.ler.yrmall.coupon.form.AvailableCouponForm;
import com.ler.yrmall.coupon.form.CouponForm;
import com.ler.yrmall.coupon.service.CouponService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


/**
 * @author Leron
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Resource
    private CouponService couponService;

    /**
     * 列表
     */
    @RequestMapping("/find")
    public Result<?> find(Form request) {
        return Result.ok(couponService.find(request));
    }


    /**
     * 信息
     */
    @RequestMapping("/getById/{id}")
    public Result<?> getById(@PathVariable("id") Long id) {
        CouponEntity coupon = couponService.get(id);
        return Result.ok(coupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result<?> save(@RequestBody CouponForm request) {
        couponService.saveCoupon(request);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result<?> update(@RequestBody CouponEntity coupon) {
        couponService.updateById(coupon);
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result<?> delete(@RequestBody Long[] ids) {
        couponService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

    // 移动端 ----

    /**
     * spu 可用的优惠券
     */
    @RequestMapping("/{spuId}")
    public Result<?> spuCoupon(@PathVariable long spuId) {
        List<CouponEntity> coupon = couponService.spuCoupon(spuId);
        return Result.ok(coupon);
    }

    /**
     * 根据 spuId 和订单金获取可用优惠券列表
     */
    @RequestMapping("/availableCoupon")
    public Result<?> availableCoupon(AvailableCouponForm request) {
        List<CouponEntity> coupon = couponService.availableCoupon(request);
        return Result.ok(coupon);
    }

    @RequestMapping("/receive/{couponId}")
    public Result<?> receive(@PathVariable long couponId) {
        couponService.receive(couponId);
        return Result.ok();
    }

    @GetMapping("/availableNum")
    public Result<?> availableNum() {
        Long count = couponService.availableNum();
        return Result.ok(count);
    }

    @GetMapping("/userCoupon")
    public Result<?> userCoupon() {
        List<CouponEntity> userCoupon = couponService.getUserCoupon(null);
        return Result.ok(userCoupon);
    }

}
